// SPDX-License-Identifier: GPL-2.0+

#include <common.h>
#include <usb.h>
#include <led.h>
#include <asm/io.h>

#include <mach/loongson.h>

#include "platform.h"

#define USB_PHY0		0x00
#define USB_PHY1		0x04
#define USB_PHY2		0x08
#define USB_PHY3		0x0C

int ls2x_set_usbphy(void)
{
	void __iomem *phy_base = (unsigned int *)PHYS_TO_UNCACHED(LS2X_USB_PHY_BASE);
	void __iomem *conf = (unsigned int *)PHYS_TO_UNCACHED(LS2X_COMMON_CONF1);
	u32 val;

	//关闭usb的预读取功能
	//usb在枚举的时候有几率枚举失败，并把ehci识别成ohci，需要把该功能关闭
	val = readl(conf);
	val = val & (~(1<<19));
	writel(val, conf);

	val = readl(phy_base + USB_PHY0);
	val = (0x1 << 25) | (0x0 << 24) | (0x3 << 22) | (0x2 << 20) | (0x2 << 18) | (0x1 << 14) | (0x3 << 10) | (0x3 << 7) | (0x7 << 4) | (0x7 << 1) | 1;
	writel(val, phy_base + USB_PHY0);

	val = readl(phy_base + USB_PHY1);
	val = (0x1 << 25) | (0x0 << 24) | (0x3 << 22) | (0x2 << 20) | (0x2 << 18) | (0x1 << 14) | (0x3 << 10) | (0x3 << 7) | (0x7 << 4) | (0x7 << 1) | 1;
	writel(val, phy_base + USB_PHY1);

	val = readl(phy_base + USB_PHY2);
	val = (0x1 << 25) | (0x0 << 24) | (0x3 << 22) | (0x2 << 20) | (0x2 << 18) | (0x1 << 14) | (0x3 << 10) | (0x3 << 7) | (0x7 << 4) | (0x7 << 1) | 1;
	writel(val, phy_base + USB_PHY2);

	val = readl(phy_base + USB_PHY3);
	val = (0x1 << 25) | (0x0 << 24) | (0x3 << 22) | (0x2 << 20) | (0x2 << 18) | (0x1 << 14) | (0x3 << 10) | (0x3 << 7) | (0x7 << 4) | (0x7 << 1) | 1;
	writel(val, phy_base + USB_PHY3);

	return 0;
}

int misc_init_r(void)
{
	return 0;
}

int ls2x_spi_fixup(void)
{
	u32 val;

	/* disable spi instruct fetch before enter spi io mode
	重置cache通路二级窗口0的基地址？ */
	val = readl((unsigned int *)PHYS_TO_UNCACHED(LS2X_CPU_WIN0_MMAP));
	if (val == 0x1fc000f2) {
		writel(0x1fc00082, (unsigned int *)PHYS_TO_UNCACHED(LS2X_CPU_WIN0_MMAP));
	}

	return 0;
}
